package ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import net.ck.hzq.R;
import net.ck.hzq.WebviewActivity;

import java.util.List;

import javax.inject.Inject;

import Bean.AreaBean;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.HeaderAndFooterRecyclerViewAdapter;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import ui.activity.home.biz.AreaBiz;
import ui.activity.home.component.DaggerAreaComponent;
import ui.activity.home.contract.AreaContract;
import ui.activity.home.module.AreaModule;
import ui.activity.home.presenter.AreaPresenter;
import ui.adapter.AreaAdapter;


/**
 * 活动专区
 * Created by chenjian on 2017/8/4.
 */

public class AreaAct extends BaseAct implements AreaContract.View {


    @Inject
    AreaPresenter presenter;
    @Inject
    AreaBiz biz;

    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    private SharedPreferences sp;

    AreaAdapter adp;


    @Override
    protected void setRoot() {
        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_area);

    }

    @Override
    protected void setup() {
        DaggerAreaComponent.builder().areaModule(new AreaModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.activityarea), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        rv.setLayoutManager(new LinearLayoutManager(AreaAct.this));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getAreaList(true, 1);
            }
        });
        rv.setOnItemClickListener(new HeaderAndFooterRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, int position) {
                Intent intent = new Intent(AreaAct.this, WebviewActivity.class);
                intent.putExtra("url", adp.getData().get(position).getImgPath());
                startActivity(intent);
            }
        });
        ptr.autoRefresh(true);
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    public void upDateUI(List<AreaBean.DataBean.ItemsBean> bean) {
        if (adp == null) {
            adp = new AreaAdapter(AreaAct.this, bean);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }


    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }

}
